﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;

namespace Manon.Core.Extensions
{


    /// <summary>
    /// Json文件读写
    /// 引用Newtonsoft.Json
    /// </summary>
    public class JsonHelper
    {

        /// <summary>
        /// 读取Json返回实体对象
        /// </summary>
        /// <returns></returns>
        public static T Read<T>(string path) => Read<T>(path, "");

        /// <summary>
        /// 根据节点读取Json返回实体对象
        /// </summary>
        /// <returns></returns>
        public static T Read<T>(string path, string section)
        {

            using (var file = new StreamReader(path))
            using (var reader = new JsonTextReader(file))
            {
                var jObj = JToken.ReadFrom(reader);
                if (jObj is Newtonsoft.Json.Linq.JArray)
                {
                    return JsonConvert.DeserializeObject<T>(jObj.ToString());
                }
                if (!string.IsNullOrWhiteSpace(section))
                {
                    var secJt = jObj[section];
                    if (secJt != null)
                    {
                        return JsonConvert.DeserializeObject<T>(secJt.ToString());
                    }
                }
                else
                {
                    return JsonConvert.DeserializeObject<T>(jObj.ToString());
                }
            }
            return default(T);
        }

        /// <summary>
        /// 读取Json返回集合
        /// </summary>
        /// <returns></returns>
        public static List<T> ReadList<T>(string path) => ReadList<T>(path, "");

        /// <summary>
        /// 根据节点读取Json返回集合
        /// </summary>
        /// <returns></returns>
        public static List<T> ReadList<T>(string path, string section)
        {

            using (var file = new StreamReader(path))
            using (var reader = new JsonTextReader(file))
            {
                var jObj = (JObject)JToken.ReadFrom(reader);
                if (!string.IsNullOrWhiteSpace(section))
                {
                    var secJt = jObj[section];
                    if (secJt != null)
                    {
                        return JsonConvert.DeserializeObject<List<T>>(secJt.ToString());
                    }
                }
                else
                {
                    return JsonConvert.DeserializeObject<List<T>>(jObj.ToString());
                }
            }

            return default(List<T>);
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        /// <typeparam name="T">自定义对象</typeparam>
        /// <param name="t"></param>
        public static void Write<T>(T t, string path) => Write(t, path, "");

        /// <summary>
        /// 写入指定section文件
        /// </summary>
        /// <typeparam name="T">自定义对象</typeparam>
        /// <param name="t"></param>
        public static void Write<T>(T t, string path, string section)
        {
            JObject jObj;
            using (StreamReader file = new StreamReader(path))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                jObj = (JObject)JToken.ReadFrom(reader);
                var json = JsonConvert.SerializeObject(t);
                if (string.IsNullOrWhiteSpace(section))
                    jObj = JObject.Parse(json);
                else
                    jObj[section] = JObject.Parse(json);
            }

            using (var writer = new StreamWriter(path))
            using (var jsonWriter = new JsonTextWriter(writer))
            {
                jObj.WriteTo(jsonWriter);
            }
        }

        /// <summary>
        /// 删除指定section节点
        /// </summary>
        /// <param name="section"></param>
        public static void Remove(string path, string section)
        {
            JObject jObj;
            using (StreamReader file = new StreamReader(path))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                jObj = (JObject)JToken.ReadFrom(reader);
                jObj.Remove(section);
            }

            using (var writer = new StreamWriter(path))
            using (var jsonWriter = new JsonTextWriter(writer))
            {
                jObj.WriteTo(jsonWriter);
            }
        }
    }
}
